"""
---------------------------------------------------------------------
- File: config.py
- Date: 2025/7/10
- Author: yangzhide
- Email: xxzjyzd@126.com
- Description: 配置管理（多环境）
---------------------------------------------------------------------
"""

import os
from pathlib import Path
from functools import lru_cache

from dotenv import load_dotenv
from pydantic_settings import BaseSettings, SettingsConfigDict

# 项目根目录
root_path = Path(__file__).parent.parent.parent
# 加载环境配置文件
load_dotenv(f"{root_path}/.env", override=True)


class Settings(BaseSettings):
    """文档相关"""

    APP_ENV: str = "dev"  # 默认开发环境
    APP_UPLOADS: str = str(root_path / "uploads")  # 默认上传目录
    APP_TITLE: str = "开发环境"
    APP_DESC: str = "开发环境描述"
    APP_DEBUG: bool = True
    APP_VERSION: str = "v1"
    APP_ADDR: str = "贵州省贵阳市"
    # 当数据表清空时，启动项目使用自动初始相关默认数据,仅在开发模式中生效
    APP_AUTO_INIT_DB: bool = True

    # 通过环境配置文件自动加载
    model_config = SettingsConfigDict(
        env_file=f"{root_path}/.env.{os.getenv('APP_ENV', 'dev')}",
        env_file_encoding="utf-8",
        extra="ignore",
    )


# 缓存配置示例
@lru_cache()
def get_settings():
    env = os.getenv("APP_ENV", "dev")
    print(f"加载环境配置文件.env.{env}")
    return Settings()


# 全局配置信息
app_settings = get_settings()
# TORTOISE_ORM 配置文件
TORTOISE_ORM = {
    "connections": {
        # Dict format for connection
        "default": {
            "engine": "tortoise.backends.asyncpg",
            "credentials": {
                "host": "localhost",
                "port": "5432",
                "user": "pg1",
                "password": "123456",
                "database": "pg1",
            },
        },
        # Using a DB_URL string
        # "default": "postgres://postgres:qwerty123@localhost:5432/events",
    },
    "apps": {
        "models": {
            # [models文件，迁移工具]
            "models": ["app.models", "aerich.models"],
            # If no default_connection specified, defaults to 'default'
            "default_connection": "default",
        }
    },
}


# token
SECRET_KEY = "yangzhide_guiyang_bs"
ALGORITHM = "HS256"
ACCESS_TOKEN_EXPIRE_MINUTES = 120
